//
//  LBCCategoryChooseV.m
//  喜马拉雅改良版框架
//
//  Created by dllo on 15/12/26.
//  Copyright © 2015年 蓝鸥科技. All rights reserved.
//

#import "LBCCategoryChooseV.h"

@interface LBCCategoryChooseV ()<UICollectionViewDelegate, UICollectionViewDataSource>

@property (nonatomic, strong)UIImageView *finishImageV;
@property (nonatomic, strong)UICollectionView *chooseV;
@property (nonatomic, assign)NSInteger indexOne;
@property (nonatomic, assign)NSInteger indexTwo;
@property (nonatomic, assign)NSInteger indexThree;


@end

@implementation LBCCategoryChooseV


-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.backgroundColor = [UIColor whiteColor];
        self.frame = CGRectMake(0, -SCREEN_HEIGHT, SCREEN_WIDTH, SCREEN_HEIGHT - 64);
         
        UIView *botView = [UIView new];
        [self addSubview:botView];
        [botView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.bottom.right.equalTo(self);
            make.height.mas_equalTo(SCREEN_HEIGHT / 667 * 120);
            
        }];
        
        self.finishImageV = [UIImageView new];
        _finishImageV.userInteractionEnabled = YES;
        _finishImageV.image = [UIImage imageNamed:@"seg_category_finish@2x.png"];
        [botView addSubview:_finishImageV];
        [_finishImageV mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(botView.mas_top).offset(20);
            make.bottom.equalTo(botView.mas_bottom).offset(-15);
            make.width.equalTo(_finishImageV.mas_height);
            make.left.mas_equalTo(SCREEN_WIDTH / 2 - 40);
            
        }];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction)];
        [_finishImageV addGestureRecognizer:tap];
        
        UILabel *nilLabel = [UILabel new];
        nilLabel.text = @"请选择类别/筛选:";
        nilLabel.textColor = [UIColor colorWithRed:0.62 green:0.62 blue:0.62 alpha:1];
        [self addSubview:nilLabel];
        [nilLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.mas_equalTo(10);
            make.height.mas_equalTo(20);
            make.right.mas_equalTo(-SCREEN_WIDTH / 5);
            
        }];
        
        UIView *lineView = [UIView new];
        lineView.backgroundColor = [UIColor colorWithRed:0.95 green:0.95 blue:0.95 alpha:1];
        [self addSubview:lineView];
        [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(10);
            make.right.mas_equalTo(-10);
            make.top.equalTo(nilLabel.mas_bottom).offset(10);
            make.height.mas_equalTo(1.5);
            
        }];
        
        UIButton *packBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        packBtn.adjustsImageWhenHighlighted = NO;
        [packBtn addTarget:self action:@selector(btnAction) forControlEvents:UIControlEventTouchUpInside];
        [packBtn setImage:[UIImage imageNamed:@"seg_pullup_n@2x.png"] forState:UIControlStateNormal];
        [self addSubview:packBtn];
        [packBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(0);
            make.bottom.equalTo(lineView.mas_top);
            make.left.equalTo(nilLabel.mas_right);
            make.right.mas_equalTo(-10);
            
        }];
        
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
        flowLayout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
        flowLayout.minimumInteritemSpacing = 5;
        flowLayout.minimumLineSpacing = 5;
        flowLayout.itemSize = CGSizeMake((SCREEN_WIDTH - 30) / 3, SCREEN_HEIGHT / 19);
        flowLayout.headerReferenceSize = CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT / 17);
        self.chooseV = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 41.5, SCREEN_WIDTH, SCREEN_WIDTH - (SCREEN_HEIGHT / 667 * 120) - 41.5) collectionViewLayout:flowLayout];
        _chooseV.backgroundColor = [UIColor whiteColor];
        // 设置允许多选
//        _chooseV.allowsMultipleSelection = YES;
        _chooseV.delegate = self;
        _chooseV.dataSource = self;
        [self addSubview:_chooseV];
        [_chooseV mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.mas_equalTo(0);
            make.top.equalTo(lineView.mas_bottom);
            make.bottom.equalTo(botView.mas_top);
            
        }];
        [_chooseV registerClass:[LBCChooseCell class] forCellWithReuseIdentifier:@"LBCChooseCell"];
        [_chooseV registerClass:[LBCChooseHeadView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"LBCChooseHeadView"];
        _indexOne = 0;
        
    }
    return self;
}


-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 3;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    if (0 == section) {
        return _tagArr.count;
    }
    return 3;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *idntifier = @"LBCChooseCell";
    LBCChooseCell *chooseCell = [collectionView dequeueReusableCellWithReuseIdentifier:idntifier forIndexPath:indexPath];
    
    if (indexPath.section == 0) {
        if (indexPath.item == _indexOne) {
            chooseCell.cateLabel.backgroundColor = [UIColor colorWithRed:0.97 green:0.39 blue:0.26 alpha:1];
            chooseCell.cateLabel.textColor = [UIColor colorWithRed:1 green:0.99 blue:0.98 alpha:1];
        } else {
            chooseCell.cateLabel.textColor = [UIColor colorWithRed:0.05 green:0.14 blue:0.26 alpha:1];
            chooseCell.cateLabel.backgroundColor = [UIColor colorWithRed:0.96 green:0.97 blue:0.98 alpha:1];
        }
        chooseCell.cateLabel.text = [_tagArr[indexPath.item] tname];
    } else if (indexPath.section == 1) {
        if (_indexOne == 0) {
            chooseCell.cateLabel.backgroundColor = [UIColor colorWithRed:0.96 green:0.97 blue:0.98 alpha:1];
            chooseCell.cateLabel.textColor = [UIColor colorWithRed:0.8 green:0.8 blue:0.8 alpha:1];
        }else if(indexPath.row != _indexTwo){
            
            chooseCell.cateLabel.textColor = [UIColor colorWithRed:0.05 green:0.14 blue:0.26 alpha:1];
            chooseCell.cateLabel.backgroundColor = [UIColor colorWithRed:0.96 green:0.97 blue:0.98 alpha:1];
        }else{
            chooseCell.cateLabel.backgroundColor = [UIColor colorWithRed:0.97 green:0.39 blue:0.26 alpha:1];
            chooseCell.cateLabel.textColor = [UIColor colorWithRed:1 green:0.99 blue:0.98 alpha:1];
        }
       
        if (indexPath.item == 0) {
            chooseCell.cateLabel.text = @"最火";
        } else if (indexPath.item == 1) {
            chooseCell.cateLabel.text = @"最近更新";
        } else {
            chooseCell.cateLabel.text = @"经典";
        }
    } else if (indexPath.section == 2){
        chooseCell.cateLabel.backgroundColor = [UIColor colorWithRed:0.96 green:0.97 blue:0.98 alpha:1];
        chooseCell.cateLabel.textColor = [UIColor colorWithRed:0.8 green:0.8 blue:0.8 alpha:1];
        if (indexPath.item == 0) {
            chooseCell.cateLabel.text = @"全部";
        } else if (indexPath.item == 1) {
            chooseCell.cateLabel.text = @"完本";
        } else {
            chooseCell.cateLabel.text = @"连载中";
        }
    }
    
    return chooseCell;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    if (kind == UICollectionElementKindSectionHeader) {
        LBCChooseHeadView *reusableView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"LBCChooseHeadView" forIndexPath:indexPath];
        reusableView.backgroundColor = [UIColor whiteColor];
        if (indexPath.section == 0) {
            reusableView.nameLabel.text = @"类别:";
        } else if (indexPath.section == 1) {
            reusableView.nameLabel.text = @"排序:";
        } else {
            reusableView.nameLabel.text = @"状态:";
        }
        return reusableView;
    }
    return nil;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        // 根据indexPath获取对应的cell
        LBCChooseCell *chooseCell = (LBCChooseCell *)[collectionView cellForItemAtIndexPath:[NSIndexPath indexPathForItem:_indexOne inSection:0]];
        chooseCell.cateLabel.textColor = [UIColor colorWithRed:0.05 green:0.14 blue:0.26 alpha:1];
        chooseCell.cateLabel.backgroundColor = [UIColor colorWithRed:0.96 green:0.97 blue:0.98 alpha:1];
        chooseCell = (LBCChooseCell *)[collectionView cellForItemAtIndexPath:indexPath];
        chooseCell.cateLabel.backgroundColor = [UIColor colorWithRed:0.97 green:0.39 blue:0.26 alpha:1];
        chooseCell.cateLabel.textColor = [UIColor colorWithRed:1 green:0.99 blue:0.98 alpha:1];
        if (indexPath.row == 0) {
            for (int i = 0; i < 3; i++) {
                 LBCChooseCell *chooseCell = (LBCChooseCell *)[collectionView cellForItemAtIndexPath:[NSIndexPath indexPathForItem:i inSection:1]];
                chooseCell.cateLabel.backgroundColor = [UIColor colorWithRed:0.96 green:0.97 blue:0.98 alpha:1];
                chooseCell.cateLabel.textColor = [UIColor colorWithRed:0.8 green:0.8 blue:0.8 alpha:1];
                
                _indexTwo = 0;
            }
        }else{
            for (int i = 0; i < 3; i++) {
                LBCChooseCell *chooseCell = (LBCChooseCell *)[collectionView cellForItemAtIndexPath:[NSIndexPath indexPathForItem:i inSection:1]];
                if (i == 0) {
                    chooseCell.cateLabel.backgroundColor = [UIColor colorWithRed:0.97 green:0.39 blue:0.26 alpha:1];
                    chooseCell.cateLabel.textColor = [UIColor colorWithRed:1 green:0.99 blue:0.98 alpha:1];
                }else{
                    chooseCell.cateLabel.textColor = [UIColor colorWithRed:0.05 green:0.14 blue:0.26 alpha:1];
                    chooseCell.cateLabel.backgroundColor = [UIColor colorWithRed:0.96 green:0.97 blue:0.98 alpha:1];
                }
            }

        }
        
        _indexOne = indexPath.item;
        
    } else if (indexPath.section == 1) {
        // 根据indexPath获取对应的cell
        LBCChooseCell *chooseCell = (LBCChooseCell *)[collectionView cellForItemAtIndexPath:[NSIndexPath indexPathForItem:_indexTwo inSection:1]];
        if (_indexOne == 0) {
            // 设置允许多选
            _chooseV.allowsMultipleSelection = NO;
        } else {
            _chooseV.allowsMultipleSelection = YES;
            chooseCell.cateLabel.textColor = [UIColor colorWithRed:0.05 green:0.14 blue:0.26 alpha:1];
            chooseCell.cateLabel.backgroundColor = [UIColor colorWithRed:0.96 green:0.97 blue:0.98 alpha:1];
            chooseCell = (LBCChooseCell *)[collectionView cellForItemAtIndexPath:indexPath];
            chooseCell.cateLabel.backgroundColor = [UIColor colorWithRed:0.97 green:0.39 blue:0.26 alpha:1];
            chooseCell.cateLabel.textColor = [UIColor colorWithRed:1 green:0.99 blue:0.98 alpha:1];
            _indexTwo = indexPath.item;
        }
    } else {
        
    }
    

    
    
}

- (void)tapAction{
    // 完成动画
    [UIView animateWithDuration:0.5 animations:^{
        self.frame = CGRectMake(0, -SCREEN_HEIGHT, SCREEN_WIDTH, SCREEN_HEIGHT - 64);
        self.bigScrollV.contentOffset = CGPointMake(SCREEN_WIDTH * _indexOne, 0);
        if (_indexOne < 2) {
            self.smallScrollV.contentOffset = CGPointMake(0, 0);
        }else{
            self.smallScrollV.contentOffset = CGPointMake(93 + 54 + (93 * _indexOne - 1) - 10, 0);
        }
        [self.categoryChooseDelegate presentToListViewController:_indexOne];
        
    }];
}

- (void)btnAction{
    // 完成动画
    [UIView animateWithDuration:0.5 animations:^{
        self.frame = CGRectMake(0, -SCREEN_HEIGHT, SCREEN_WIDTH, SCREEN_HEIGHT - 64);
        
    }];
}




/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
